package com.scau.ums.controller;

import com.scau.ums.mapper.LostandfoundinfoMapper;
import com.scau.ums.mapper.StudentMapper;
import com.scau.ums.mapper.TeacherMapper;
import com.scau.ums.model.Lostandfoundinfo;
import com.scau.ums.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;

/**
 * 描述：处理用户填写失物招领表单的控制类
 * @author xfy
 * @version 1.0
 * @date 2019/7/25 9:26
 */

@Controller
public class FillLostFormController {
    @Autowired
    StudentMapper studentMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    LostandfoundinfoMapper lostandfoundinfoMapper;

//    /**
//     * 描述：处理用户填写失物招领表单的控制器，前提需要登录，且只有学生、教师才可以使用该功能
//     * @param request
//     * @return
//     */
//    @RequestMapping("/fillLostFormTemp")
//    public String tempPost(HttpServletRequest request){
//        //设置session
//        HttpSession session = request.getSession();
//        User user;
//        //判断用户类型
//        if(session.getAttribute("USER_TYPE").equals(new Integer(User.STUDENT))){
//            user = studentMapper.selectByPrimaryKey((String)session.getAttribute("USER_ID"));
//            session.setAttribute("USER_NAME",user.getName());
//        }
//        else if(session.getAttribute("USER_TYPE").equals(new Integer(User.TEACHER))){
//            user = teacherMapper.selectByPrimaryKey((String)session.getAttribute("USER_ID"));
//            session.setAttribute("USER_NAME",user.getName());
//        }
//        return "fillLostForm";
//    }

    @RequestMapping(value = "/fillLostForm", method = RequestMethod.GET)
    public String fillLostFormGet(){
        return "fillLostForm";
    }

    @ResponseBody
    @RequestMapping(value = "/fillLostForm", method = RequestMethod.POST)
    public String fillLostFormPost(HttpServletRequest request, HttpServletResponse response) throws ParseException, SQLException, IOException {
        HttpSession session = request.getSession();
        PrintWriter printWriter = response.getWriter();
        request.setCharacterEncoding("utf-8");

        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-type", "text/html;charset=utf-8");
        String msg = "";
        try{
            //处理前端传来的数据，若是空“”要转为null，使数据库不能插入
            String itemName = request.getParameter("itemName");
            if(itemName.equals("")) itemName = null;
            String itemDesc = request.getParameter("itemDesc");
            if(itemDesc.equals("")) itemDesc = null;
            String telephone = request.getParameter("telephone");
            if(telephone.equals("")) telephone = null;

            Date dateTime = new Date();
            //给失物招领信息赋值
            Lostandfoundinfo lostForm = new Lostandfoundinfo();
            lostForm.setPublishername((String)session.getAttribute("USER_NAME"));
            lostForm.setPublisherid((String)session.getAttribute("USER_ID"));
            Integer userType = (Integer) session.getAttribute("USER_TYPE");
            if(userType.equals(new Integer(User.STUDENT))){
                lostForm.setPublishertype("学生");
            }
            else if(userType.equals(new Integer(User.TEACHER))){
                lostForm.setPublishertype("老师");
            }

            lostForm.setIsfounded("否");
            lostForm.setState("审核中");
            lostForm.setItemname(itemName);
            lostForm.setItemdesc(itemDesc);
            lostForm.setTelephone(telephone);
            lostForm.setPublishdate(dateTime);

            //往数据库插入一条失物招领信息
            lostandfoundinfoMapper.insertSelective(lostForm);
        }catch (Exception e){
            System.out.println("error occurred");
            msg += "<script>alert('failed');window.history.go(-1);</script>";
            printWriter.println(msg);
            printWriter.close();
            return "fillLostForm";
        }

        System.out.println("fill the form");
        msg += "<script>alert('successfully');window.location.href='fillLostForm';</script>";
        printWriter.println(msg);
        printWriter.close();
        return "fillLostForm";
    }
}
